package pingan.com.quarter_hour.acyivity.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import pingan.com.quarter_hour.R;
import pingan.com.quarter_hour.bean.LoginSuccessBean;
import pingan.com.quarter_hour.presenter.UserPresenter;
import pingan.com.quarter_hour.view.IView;

public class ZhuCeActivity extends AppCompatActivity implements View.OnClickListener, IView {

    private static final String TAG = "ZhuCeActivity";
    @BindView(R.id.zhcue)
    TextView zhcue;
    @BindView(R.id.myname)
    EditText myname;
    @BindView(R.id.mypassword)
    EditText mypassword;
    @BindView(R.id.alogin)
    TextView alogin;
    private UserPresenter userPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhu_ce);
        ButterKnife.bind(this);

        zhcue.setOnClickListener(this);
        alogin.setOnClickListener(this);

        userPresenter = new UserPresenter();
        userPresenter.attachView(this);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.zhcue:
                Intent intent = new Intent(ZhuCeActivity.this, RegisteredActivity.class);
                finish();
                startActivity(intent);

                break;
            case R.id.alogin:
                String trim = myname.getText().toString().trim();
                String pass = mypassword.getText().toString().trim();

                HashMap<String, String> map = new HashMap<>();
                map.put("mobile", trim);
                map.put("password", pass);
                //登录接口   https://www.zhaoapi.cn/user/login?mobile=13161666666&password=123123
                userPresenter.getLoginSuccess(" https://www.zhaoapi.cn/", map);
                Toast.makeText(ZhuCeActivity.this, "点击了", Toast.LENGTH_SHORT).show();
                break;
        }
    }


    @Override
    public void success(Object o) {
        LoginSuccessBean news = (LoginSuccessBean) o;
        if (news != null) {
            Log.i(TAG, "success: " + news.getMsg());
        }
    }

    @Override
    public void Failes(Exception e) {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (userPresenter != null) {
            userPresenter.delete();
        }
    }
}
